package example.jaxws;

import datamodel.Author;
import datamodel.MetaInfo;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import java.io.FileOutputStream;
import java.util.LinkedList;

/**
 * Created by IntelliJ IDEA.
 * User: Qiang
 * Date: 27.11.2008
 * Time: 16:33:18
 */
public class CreaterXML {
    public static String MSNS = "http://schemas.microsoft.com/office/word/2004/10/bibliography";

    Document document = new Document();

    public static void create() {
        String filepath = "D:\\glassfish\\domains\\domain1\\docroot\\result.xml";
        try {
            // Build w/ SAX and JAXP, no validation
            // SAXBuilder b = new SAXBuilder();
            // create the document
            // Document doc = b.build(new File(filepath));
            Document doc = new Document();

            Element root = new Element("Root").setText("something");
            doc.setRootElement(root);
            Namespace msns = Namespace.getNamespace("b", MSNS);
            root.addNamespaceDeclaration(msns);

            Author author = new Author("Qiang2", "Li2");
            Author qiangli = new Author("Qiang", "Li");
            Author meiyang = new Author("Mei", "Yang");

            LinkedList<Author> authorList = new LinkedList<Author>();
            authorList.add(qiangli);
            authorList.add(meiyang);
            authorList.add(author);
            MetaInfo metaInfo = new MetaInfo("tag01", "A book for test", "Article", "2008","Nanchang", "verlager", "F:/test.xml", authorList);

            Element authorEl = metaInfo.toXMLElement(msns);
            root.addContent(authorEl);

            XMLOutputter outputter = new XMLOutputter();
            Format fmt = Format.getPrettyFormat();
            fmt.setIndent("  ");
            outputter.setFormat(fmt);


            FileOutputStream fo = new FileOutputStream("D:\\glassfish\\domains\\domain1\\docroot\\test1.xml");
            outputter.output(doc, fo);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
